let indexed = {
  data: {},
  // 取数据
  // id 本条数据的id
  // fun 回调函数
  indexedDB_get: function (username, id, fun) {
    if (!window.indexedDB) {
      console.log("浏览器不支持indexedDB");
      var _data = {
        code: 1,
        data: '',
        message: '浏览器不支持indexedDB'
      };
      fun(_data); // 回调
    } else {
      //console.log('支持indexedDB');
      // 创建变量
      var db;
      // 创建数据库的名字 nim_db_ + usrname
      const dbName = "nim_db_" + username;
      // 连接创建的数据库
      var request = window.indexedDB.open(dbName);
      // 本地数据库返回错误
      request.onerror = function (event) {
        // Handle errors.
        //console.log('error',event);
        var _data = {
          code: 2,
          data: event,
          message: '连接数据库失败'
        };
        fun(_data); // 回调
      };
      // 连接数据库成功
      request.onsuccess = function (event) {
        db = event.target.result;
        //console.log('db链接成功get:',db);

        var objectStore = db.transaction(["nim_datas"], "readwrite").objectStore("nim_datas");
        var request = objectStore.get(id);
        request.onerror = function (event) {
          // Handle errors!
          var _data = {
            code: 3,
            data: event,
            message: '查询数据失败'
          };
          fun(_data); // 回调
        };
        request.onsuccess = function (event) {
          // Get the old value that we want to update
          var result = event.target.result;
          // console.log('get原数据',result);
          if (!result) {
            result = {
              myapp: id,
              data: ''
            };
          }
          var _data = {
            code: 0,
            data: result,
            message: '查询数据成功'
          };
          fun(_data); // 回调
        };
      };
      //该句柄在我们请求打开的数据库的版本号和已经存在的数据库版本号不一致的时候调用。
      request.onupgradeneeded = function (event) {
        db = event.target.result;
        //console.log('db:',db);

        var objectStore = db.createObjectStore("nim_datas", {keyPath: "myapp"});
      };
    }
  },
  // 添加、更新数据
  // id 本条数据的id
  // dataStr 需要存储的数据
  // fun 回调函数
  indexedDB_add: function (username, id, dataStr, fun) {
    if (!window.indexedDB) {
      console.log("浏览器不支持indexedDB");
      var _data = {
        code: 1,
        data: '',
        message: '浏览器不支持indexedDB'
      };
      fun(_data); // 回调
    } else {
      //console.log('支持indexedDB');
      var db;
      const dbName = "nim_db_" + username;
      var request = window.indexedDB.open(dbName);
      request.onerror = function (event) {
        // Handle errors.
        console.log('error', event);
        var _data = {
          code: 2,
          data: event,
          message: '连接数据库失败'
        };
        fun(_data); // 回调
      };
      request.onsuccess = function (event) {
        db = event.target.result;
        //console.log('db链接成功add:',db);
        //console.log(db);
        var objectStore = db.transaction(["nim_datas"], "readwrite").objectStore("nim_datas");
        //console.log(objectStore);
        var request = objectStore.get(id);
        request.onerror = function (event) {
          // Handle errors!
          var _data = {
            code: 3,
            data: event,
            message: '查询数据失败'
          };
          fun(_data); // 回调
        };
        request.onsuccess = function (event) {
          // Get the old value that we want to update
          var _data = event.target.result;
          // console.log('add原数据',_data);
          if (_data) {
            _data.data = dataStr;
          } else {
            _data = {
              myapp: id,
              data: dataStr
            };
          }
          // Put this updated object back into the database.
          var requestUpdate = objectStore.put(_data);
          requestUpdate.onerror = function (event) {
            // Do something with the error
            var _data = {
              code: 4,
              data: event,
              message: '更新数据失败'
            };
            fun(_data); // 回调
          };
          requestUpdate.onsuccess = function (event) {
            // Success - the data is updated!
            //console.log('更新成功');
            var _data = {
              code: 0,
              data: event.target.result,
              message: '更新数据成功'
            };
            fun(_data); // 回调
          };
        };
      };
      //该句柄在我们请求打开的数据库的版本号和已经存在的数据库版本号不一致的时候调用。
      request.onupgradeneeded = function (event) {
        db = event.target.result;
        //console.log('db:',db);

        var objectStore = db.createObjectStore("nim_datas", {keyPath: "myapp"});
      };
    }
  }

};
export default indexed;
